package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCollect;
import com.cskaoyan.bean.MarketCollectExample;
import com.cskaoyan.bean.MarketOrder;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCollectMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class AdminCollectServiceImpl implements AdminCollectService {
    @Override
    public CommonData<MarketOrder> list(Integer userId, Integer valueId, Integer page, Integer limit, String sort, String order) {

        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        if (userId != null){

            criteria.andUserIdEqualTo(userId);


        }

        if (valueId != null){

            criteria.andValueIdEqualTo(valueId);


        }

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {

            marketCollectExample.setOrderByClause(sort + " " + order);
        }

        SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();

        SqlSession sqlSession = sqlSessionFactory.openSession();


        MarketCollectMapper mapper = sqlSession.getMapper(MarketCollectMapper.class);



        PageHelper.startPage(page, limit);

        List <MarketCollect> marketCollects = mapper.selectByExample(marketCollectExample);


        if (sqlSession!=null){

            sqlSession.close();

        }
        return CommonData.data(new PageInfo(marketCollects));


    }
}
